package {
	import com.util.FpsMeter;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	
	import leaf.science.Global;
	import leaf.science.nav.Page;
	import leaf.science.pages.PageDemos;
	import leaf.science.pages.PageFeatures;
	import leaf.science.pages.PageIntro;
	import leaf.science.pages.PageSpecifications;
	import leaf.science.ui.camera.DemosCamera;
	import leaf.science.ui.jogwheel.JogWheel;
	import leaf.science.ui.leftmenu.LeftMenu;
	import leaf.science.ui.topbar.TopBar;
	
	
	[SWF(frameRate="60", backgroundColor="#ECECEC", width="1024", height="680")]
	[Frame(factoryClass="LeafScienceLoader")]
	public class LeafScience extends MovieClip
	{
		
		private var _bg:Sprite;
		private var _topBar:TopBar;
		private var _pages:Object;
		private var _pagesContainer:Sprite;
		private var _currentPage:Page;
		private var _fps:FpsMeter;
		
		public var nextPage:Page;
		public var prevPageName:String = "";
		
		public static var JOGWHEEL:JogWheel;
		public static var CAMERA:DemosCamera;
		public static var LEFT_MENU:LeftMenu;
		
		public static var instance:LeafScience;
		
		public function LeafScience()
		{
			instance = this;
			addEventListener(Event.ADDED_TO_STAGE, setupStage);
		}
		
		public function get currentPage ():Page
		{
			return _currentPage;
		}
		
		public function setAddress (value:String):void
		{
			if (_pages[value]);
			SWFAddress.setValue('science/' + value );
		}
		
		private function onAddressChange(evt:SWFAddressEvent):void
		{
			var address:String = parseAdress(evt.value);
			if (address != "")
			gotoPage(address);
			else setAddress('intro');
		}
		
		
		/**
		 * sets the current page 
		 * @param value	either an IPage or a page name (String)
		 * 
		 */		
		private function gotoPage (value:Object):void
		{
			if (value is Page) nextPage = value as Page;
			else if (value is String){
				if (_pages[value])
				nextPage = _pages[value];
				else return;
			}
			SWFAddress.setTitle("Leaf Photography | Science | " + nextPage.toString());			
			if (_currentPage){
				_currentPage.onOutComplete = onPageOutComplete;
				_currentPage.goOut();
			} else {
				onPageOutComplete();
			}
		}
		
		private function onPageOutComplete ():void
		{
			if (_currentPage && _pagesContainer.contains(_currentPage)) 
			_pagesContainer.removeChild(_currentPage);
			prevPageName = _currentPage ? _currentPage.toString() : '';
			_currentPage = nextPage;
			_pagesContainer.addChild(nextPage);
			onResize();
		}
		
		
		private function createPages():void
		{
			// refferences to pages by name:
			_pages = {
				intro: 				new PageIntro(),
				demos:				new PageDemos(),
				features:			new PageFeatures(),
				specifications: 	new PageSpecifications()
			}
		}
		
		private function parseAdress (address:String):String
		{
			return address.split('/')[2] || '';
		}
		
		private function onResize (evt:Event = null):void
		{
			_bg.x = stage.stageWidth / 2;
			_bg.y = stage.stageHeight / 2;
			
			_topBar.width = stage.stageWidth;
			
			if (_currentPage){
				_currentPage.onResize();
			}
		}
		
		
		private function setupStage (evt:Event):void
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.showDefaultContextMenu = false;
			stage.addEventListener(Event.RESIZE, onResize);
			
			if (loaderInfo.parameters.root_path != undefined)
			Global.ROOT_PATH = loaderInfo.parameters.root_path;
			trace(Global.ROOT_PATH, loaderInfo.parameters.root_path);
			
			_bg = new LeafBgRadial();
			addChild(_bg);
			
			_pagesContainer = new Sprite();
			addChild(_pagesContainer);
			
			_topBar = new TopBar();
			addChild(_topBar);
			
//			_fps = new FpsMeter();
//			addChild(_fps);
//			addEventListener('enterFrame', function (evt:Event):void{_fps.update()})
			
			onResize();
			createPages();
			
			_topBar.goIn();
			
			// emulate deep linking:
//			SWFAddress.setValue('science/features')
			//
			
			SWFAddress.addEventListener('change', onAddressChange);
			
		}
	}
}
